from lib.clock import rtc_clock
from lib.sensors.oled import ops
from machine import Timer
from lib.config.index import sensor_config

oled_config = sensor_config.get_sensor("ssd1306")
scl = oled_config["scl"]
sda = oled_config["sda"]
width = oled_config["width"]
height = oled_config["height"]

clock = rtc_clock.RTC_CLOCK()
clock.sync_network_time()

tim0 = Timer(0)
tim1 = Timer(1)

sync_inteval = 60 * 1000 * 60 * 12
tim1.init(
    period=sync_inteval,
    mode=Timer.PERIODIC,
    callback=lambda t: clock.sync_network_time(),
)


ops.init_i2c(scl, sda, width, height)


def show_time():
    # print(clock.get_date_str(),clock.get_time_str())
    ops.rect(0, 0, 128, 15, 0)
    # print(clock.get_time_str())
    ops.text(clock.get_date_str(), 0, 0)
    ops.text(clock.get_time_str(), 0, 10)
    ops.show()


tim0.init(period=1000, mode=Timer.PERIODIC, callback=lambda t: show_time())
